#ifndef __PLAYER_H
#define __PLAYER_H

/*

	PLAYER.H

	The CPlayer class

	OpenGL Game Programming
	Author: Kevin Hawkins
	Date: 3/30/2001
	Description:

*/

#include <typeinfo.h>
#include "DirectXAudio.h"
#include "sod.h"
#include "ogro.h"
#include "rocket.h"
#include "bfg.h"
#include "sniper.h"
#include "camera.h"
#include "object.h"
#include "terrain.h"
#include "world.h"

class CPlayer : public CObject
{
private:
	CCamera *camera;
	CTerrain *terrain;
	CDirectXAudio *audioSys;
	
protected:
	int hitPoints;
	int hit;
	bool alive;
	int gun;
void OnCollision(CObject *collisionObject)
	{
		if (typeid(*collisionObject) == typeid(CTerrain))
		{
			// position.y = ((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size;
			float a = position.y;
			float b = ((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size;
			// std::cout << "? " << a << ' ' << b << '\n';
			position.y = std::max( a, b );
		}
		else if (typeid(*collisionObject) == typeid(COgroEnemy))
		{
			if (hit < 0)
			{
				takeDamge(10);
				hit = 5;
			}

			if (camera)
			{
				if (camera->velocity.z >= 0.0)
					camera->velocity = CVector(0.0, -100.0, -100.0);
					
			}
		}
		else if (typeid(*collisionObject) == typeid(CSodEnemy))
		{
			if (camera)
			{
				if (camera->velocity.z > 0.0)
					camera->velocity = CVector(0.0, 0.0, 0.0);
			}
		}
		else if (typeid(*collisionObject) == typeid(CPlayer))
		{
		}
	}

	void OnPrepare()
	{
		if (camera)
		{
			position = camera->position;		// the player stays with the camera
			direction = camera->yaw;
			pitch = camera->pitch;
		}
		
	}

public:
	float direction;	// direction player is facing (same as camera)
	float pitch;		// pitch of player's lookAt vector
	
	CPlayer() 
	{ 
		size = 7.0f; 
		camera = NULL; 
		terrain = NULL; 
		audioSys = NULL; 
		hitPoints = 100;
		alive = true;
		hit = 100;
		gun = 1;
	}
	~CPlayer() {}

	void SetCamera(CCamera *c) { camera = c; }
	void DetachCamera() { camera = NULL; }

	void SetTerrain(CTerrain *t) { terrain = t; }
	void FireWeapon();
	void SetAudioSystem(CDirectXAudio *aSys) { audioSys = aSys; }

	void OnAnimate(scalar_t deltaTime) {
		// camera->velocity.y -= deltaTime * 10;
		// camera->velocity.x -= deltaTime * 10;
		// camera->acceleration.y -= deltaTime * 100;
		// camera->acceleration.x -= deltaTime * 100;

		const auto& a = camera->position;
		const auto& b = camera->velocity;
		// std::cout << '(' << a.x << ", " << a.y << ", " << a.z << ") <"
		//	<< b.x << ", " << b.y << ", " << b.z << ">\n";
		CObject::OnAnimate(deltaTime);
	}

    void takeDamge(int damage)
	{
		 hitPoints -= damage;
		 if (hitPoints < 1)
		 {
			 FireWeapon();
			 alive = false;
		 }
	}

	int getHitPoints()
	{
		return hitPoints;
	}

	void hitCountDown()
	{
		hit--;
	}

	void changeGun(int x)
	{
		gun = x;
	}

	int getGun()
	{
		return gun;
	}
};

#endif